﻿/*
 * Created by SharpDevelop.
 * User: Christian
 * Date: 17.02.2011
 * Time: 19:26
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using ThatsMeSDK.Core;

namespace ThatsMeSDK
{
	/// <summary>
	/// Description of UserBase.
	/// </summary>
	public abstract class UserBase : MemberBase, IUser
	{
		public UserBase()
		{}
		
		public UserBase(string nick, string password)
		{
			Nick = nick;
			Password = password;
        }

	    public bool LogIn()
		{
	    	//Just trycatch if release
	    	#if RELEASE
            try
            {
            #endif
#warning Token Not Handled
            Token = null; //ServiceHandler.Authenticate(Nick, Password);
                if(!string.IsNullOrEmpty(Token))
                {
	        		LoadMyMailBox();
                    return true;
                }
                
	    	#if RELEASE
            }
            catch (Exception)
            {
                return false;
            }
            #endif

            return false;
		}

        public MailBox UserMailBox { get; private set; }
       
	
        #region IUser Member
        private string password;

        public string Password 
        {
        	get{return password;}
        	internal set{ if(value != null) password = ServiceHandler.CalculateMD5(value);}
        }
        
        public void SetRudePasswordHash(string passwordHash)
        {
        	this.password = passwordHash;
        }

        #endregion

        #region IUser Member


        public string Token { get;private set;}

	    public void LoadMyMailBox()
        {
            UserMailBox = new MailBox(Token);
	    }
	    
	    public override void LoadMyUser()
	    {
            //Todo: Switch Member to User!
#warning User returns null! Please make it asynchron
            User me = null; //ServiceHandler.GetMemberByNick(Nick).ConvertToUser();
	    	Id = me.Id;
	    	Birthday = me.Birthday;
	    	Canton = me.Canton;
	    	Sex = me.Sex;
	    	ProfilePictureUri = me.ProfilePictureUri;
	    	ProfileUri = me.ProfileUri;
	    	//RealName = me.RealName;
	    	//Friends = me.Friends;
	    	//Description = me.Description;
	    }

	    #endregion
	}
}
